<?php
/**
 * Display the list of Medical Centers where the current
 * user is attached as a partner
 *
 * @author Xavier VILAIN
 * @package Nawa\MedicalCenters
 * @license
 */

use Fox\Generator;
use Fox\Html\Block;
use Fox\Grid;
use Nawa\Messages;
$medicalCenters = User::current()->contact()->medicalCenters();

$grid = new Grid('gMyMedicalCenters');
$grid->fieldcat(array('ms_reference',array('mc_reference_intern','reference'),'name1','name2','udate'));
$grid->fieldcat()->switchToObjectMode();
$grid->fieldcat()->field('udate')->displayTime();
$grid->fieldcat()->setGenerators(array(
	'reference' => Generator::useFunction(function($data) {
		$mc = $data->medicalCenterPartner()->medicalCenter();
		$data->name1 = $mc->name1;
		$data->name2 = $mc->name2;
		return $mc->reference;
	}),
	'ms_reference' => Generator::useFunction(function ($data){
				return $data->medicalCenterPartner()->medicalStudy()->reference;
			})
));
$grid->fieldcat()->field('ms_reference')
			->label(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::LABEL_STUDY))
			;

$grid->onClick(Generator::location(Generator::url('medicalstudies','medicalcenter',null,
array('msid' => Generator::useAttribute('msid'),'mcid' => Generator::useAttribute('mcid')))));
$grid->initialize($medicalCenters);
if(Xview::instance()->ajax())
{
	echo $grid->display();
}
else
{
	$block = new Block('bMedicalCenters',Msg(Messages::BUNDLE,Messages::LABEL_MY_MEDICAL_CENTERS));
	$block->collapsed(false);
	$block->add($grid);
	return $block;
}